package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.SearchViewItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.SearchViewResponse;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.GlideRoundTransform;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/13 13:35
 * description:
 */
public class SearchViewAdapter extends BaseRecyclerAdapter<SearchViewAdapter.SearchViewHolder> {

    private Context mContext;

    private List<SearchViewResponse.SpotDeatilSpotListBean> listBeen;
    private List<SearchViewResponse.SpotDeatilHotelListBean> hotelData;

    public SearchViewAdapter(Context context) {
        mContext = context;

    }

//    @Override
//    public int getItemViewType(int position) {
//        return R.layout.search_view_item;
//    }

    @Override
    public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return SearchViewHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(SearchViewHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, SearchViewItemBinding binding) {
        if (hotelData != null && hotelData.size() > 0) {
            final SearchViewResponse.SpotDeatilHotelListBean spotDeatilHotelListBean = hotelData.get(position);
            binding.alltypeAdress.setText(spotDeatilHotelListBean.getAddress());
            binding.alltypeDistance.setText(spotDeatilHotelListBean.getIntroduce());
            binding.alltypeShopname.setText(spotDeatilHotelListBean.getName());
            binding.alltypeTell.setText(spotDeatilHotelListBean.getTelephone());
            binding.allTypeRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String access_token = ACache.get(mContext).getAsString("access_token");
                    String url = Constant.BASE_URL + "app-front-end/#/card/goSpotInfoApp?access_token=" + access_token + "&spot_id=" + spotDeatilHotelListBean.getSpot_id();
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.TITLE, "");
                    bundle.putString("url", url);
                    JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                }
            });
            String pic_url = spotDeatilHotelListBean.getPic_url();
            Glide.with(mContext)
                    .load(pic_url)
                    .error(R.drawable.zhanweitu)
                    .placeholder(R.drawable.zhanweitu)
                    .centerCrop()
                    .override(100, 100)
                    .transform(new GlideRoundTransform(mContext))
                    .into(binding.sev);

        }
//        binding.alltypeShopname.setText(hotelData.get(position).getName());
//        binding.alltypeAdress.setText(hotelData.get(position).getAddress());
    }

    @Override
    public int getItemCount() {
        return null == hotelData ? 0 : hotelData.size();
    }

    public void setViewData(List<SearchViewResponse.SpotDeatilSpotListBean> spot_deatil_spot_list) {
        if (spot_deatil_spot_list != null) {
            listBeen = spot_deatil_spot_list;
        }
    }

    public void setHotelData(List<SearchViewResponse.SpotDeatilHotelListBean> hotelData) {
        this.hotelData = hotelData;
    }

    public static class SearchViewHolder extends BaseHolder {

        private SearchViewItemBinding binding;

        public static SearchViewHolder creat(ViewGroup parent) {
            SearchViewItemBinding binding = SearchViewItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new SearchViewHolder(binding);
        }

        public SearchViewHolder(SearchViewItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
